/*
 *                                                     __----~~~~~~~~~~~------___
 *                                    .  .   ~~//====......          __--~ ~~
 *                    -.            \_|//     |||\\  ~~~~~~::::... /~
 *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
 *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
 *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *           '         ~-|      /|    |-~\~~       __--~~
 *                       |-~~-_/ |    |   ~\_   _-~            /\
 *                            /  \     \__   \/~                \__
 *                        _--~ _/ | .-~~____--~-/                  ~~==.
 *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                  -_     ~\      ~~---l__i__i__i--~~_/
 *                                  _-~-__   ~)  \--______________--~~
 *                                //.-~~~-~_--~- |-------~~~~~~~~
 *                                       //.-~~~--\
 *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * 
 *                               神兽保佑            永无BUG
 */


#include "RUI_ROOT_INIT.h"

/************************************************************万能分隔符**************************************************************
 * 	@author:			//小瑞
 *	@performance:	    //ROOT函数
 *	@parameter:		    //
 *	@time:				//23-04-13 17:38
 *	@ReadMe:			//
 ************************************************************万能分隔符**************************************************************/
void RUI_F_ROOT(RUI_ROOT_STATUS_Typedef *Root, MOTOR_Typdef *MOTOR)
{
    RUI_F_ROOT_ALL_STATUS(Root, MOTOR);
    RUI_F_MASTER_LOCATION_JUDGE(Root);
    RUI_F_LED_SHOW_STATUS(Root);
}
/************************************************************万能分隔符**************************************************************
// * 	@author:			//WHW
// *	@performance:	    //大疆电机离线检测
// *	@parameter:		    //
// *	@time:				//23-04-13 15:01
// *	@ReadMe:			//
// ************************************************************万能分隔符**************************************************************/
uint8_t DJI_F_MOTOR_STATUS(DJI_MOTOR_DATA_Typedef* DATA)
{
    DATA->ONLINE_JUDGE_TIME--;

    if (DATA->ONLINE_JUDGE_TIME < 5)
    {
        DATA->ONLINE_JUDGE_TIME = 0;
        return RUI_DF_OFFLINE;
    }
    else
        return RUI_DF_ONLINE;
}

/************************************************************万能分隔符**************************************************************
// * 	@author:			//WHW
// *	@performance:	    //达妙电机离线检测
// *	@parameter:		    //
// *	@time:				//23-04-13 15:01
// *	@ReadMe:			//
************************************************************万能分隔符**************************************************************/
uint8_t DM_F_MOTOR_STATUS(DM_MOTOR_DATA_Typdef* DATA)
{
    DATA->ONLINE_JUDGE_TIME--;

    if (DATA->ONLINE_JUDGE_TIME < 5)
    {
        DATA->ONLINE_JUDGE_TIME = 0;
        return RUI_DF_OFFLINE;
    }
    else
        return RUI_DF_ONLINE;
}


/************************************************************万能分隔符**************************************************************
 * 	@author:			//小瑞
 *	@performance:	    //总的离线检测
 *	@parameter:		    //
 *	@time:				//23-03-29 19:34
 *	@ReadMe:			//
 *  @LastUpDataTime:    //2023-04-13 15:22    bestrui
 *  @UpData：           //电机检测封装成函数
 ************************************************************万能分隔符**************************************************************/
void RUI_F_ROOT_ALL_STATUS(RUI_ROOT_STATUS_Typedef *Root, MOTOR_Typdef *MOTOR)
{
    //主机在线监测

	//电机在线检测
    Root->MOTOR_BOTTOM_CHASSIS_1 = DJI_F_MOTOR_STATUS(&MOTOR->DJI_3508_CHASSIS_1.DATA);
    Root->MOTOR_BOTTOM_CHASSIS_2 = DJI_F_MOTOR_STATUS(&MOTOR->DJI_3508_CHASSIS_2.DATA);
    Root->MOTOR_BOTTOM_CHASSIS_3 = DJI_F_MOTOR_STATUS(&MOTOR->DJI_3508_CHASSIS_3.DATA);
    Root->MOTOR_BOTTOM_CHASSIS_4 = DJI_F_MOTOR_STATUS(&MOTOR->DJI_3508_CHASSIS_4.DATA);

}

/************************************************************万能分隔符**************************************************************
 * 	@author:			//小瑞
 *	@performance:	    //主控位置判断
 *	@parameter:		    //
 *	@time:				//23-03-29 19:34
 *	@ReadMe:			//
 ************************************************************万能分隔符**************************************************************/
uint8_t RUI_F_MASTER_LOCATION_JUDGE(RUI_ROOT_STATUS_Typedef *Root)
{
    Root->MASTER_LOCATION = RUI_DF_MASTER_LOCATION_BOTTOM;
	return RUI_DF_READY;        
}

/************************************************************万能分隔符**************************************************************
 * 	@author:			//小瑞
 *	@performance:	    //LED
 *	@parameter:		    //
 *	@time:				//23-04-13 16:05
 *	@ReadMe:			//颜色参考RUI_DEFINE.h文件中的颜色 TIME小于10时视觉效果为长亮
 ************************************************************万能分隔符**************************************************************/
void RUI_F_LED(uint8_t COLOR , int TIME)
{
	static uint32_t TIME_LAST = 0;
	
    uint32_t TIME_NOW = (uint32_t)DWT_GetTimeline_ms();

    static int8_t BIT = 0 , status_LAST = 9;

    static GPIO_TypeDef* LED_PORT[3] ={ RED_GPIO_Port, GREEN_GPIO_Port, BLUE_GPIO_Port };

    static uint16_t LED_PIN[3] ={ RED_Pin, GREEN_Pin, BLUE_Pin };

    static uint8_t LED_CONFIG[6][3] ={
        {1, 0, 0},    // RED
        {0, 1, 0},    // GREEN
        {0, 0, 1},    // BLUE
        {1, 1, 0},    // YELLOW
        {1, 0, 1},    // PURPLE
        {0, 1, 1},    // CYAN
    };

    if (COLOR != status_LAST)
    {
        for (int i = 0; i < 3; i++)
        {
            HAL_GPIO_WritePin(LED_PORT[i] , LED_PIN[i] , GPIO_PIN_SET);
        }
        status_LAST = (int8_t)COLOR;
    }

    if ((TIME_NOW - TIME_LAST) > TIME)
    {
        BIT = !BIT;
        TIME_LAST = TIME_NOW;
    }

    for (int i = 0; i < 3; i++)
    {
        if (LED_CONFIG[COLOR][i])
        {
			if(BIT == 0)
				HAL_GPIO_WritePin(LED_PORT[i] , LED_PIN[i] , GPIO_PIN_RESET);
			if(BIT > 0)
				HAL_GPIO_WritePin(LED_PORT[i] , LED_PIN[i] , GPIO_PIN_SET);
        }
    }
}

/************************************************************万能分隔符**************************************************************
 * 	@author:			//小瑞
 *	@performance:	    //用LED灯显示状态
 *	@parameter:		    //
 *	@time:				//23-04-13 17:36
 *	@ReadMe:			//
 *  @LastUpDataTime:    //2023-05-07 17:03    bestrui
 *  @UpData：           //底盘正常改为绿灯闪烁
 ************************************************************万能分隔符**************************************************************/
void RUI_F_LED_SHOW_STATUS(RUI_ROOT_STATUS_Typedef *Root)
{
    switch (Root->MASTER_LOCATION)
    {
		case RUI_DF_MASTER_LOCATION_JUDGEING:
            RUI_F_LED(RUI_DF_LED_YELLOW , 100);
            break;

        case RUI_DF_MASTER_LOCATION_BOTTOM:
        {
            if (Root->RM_Master                 == RUI_DF_ONLINE &&
            Root->MOTOR_BOTTOM_CHASSIS_1        == RUI_DF_ONLINE &&
            Root->MOTOR_BOTTOM_CHASSIS_2        == RUI_DF_ONLINE &&
            Root->MOTOR_BOTTOM_CHASSIS_3        == RUI_DF_ONLINE &&
            Root->MOTOR_BOTTOM_CHASSIS_4		== RUI_DF_ONLINE)
            {
                RUI_F_LED(RUI_DF_LED_CYAN , 200);
            }
            if (Root->RM_Master                 == RUI_DF_OFFLINE &&
            (Root->MOTOR_BOTTOM_CHASSIS_1       == RUI_DF_OFFLINE ||
            Root->MOTOR_BOTTOM_CHASSIS_2        == RUI_DF_OFFLINE ||
            Root->MOTOR_BOTTOM_CHASSIS_3        == RUI_DF_OFFLINE ||
            Root->MOTOR_BOTTOM_CHASSIS_4		== RUI_DF_OFFLINE))
            {
                RUI_F_LED(RUI_DF_LED_PURPLE , 50);
            }
            if (Root->RM_Master                 == RUI_DF_ONLINE &&
            (Root->MOTOR_BOTTOM_CHASSIS_1       == RUI_DF_OFFLINE ||
            Root->MOTOR_BOTTOM_CHASSIS_2        == RUI_DF_OFFLINE ||
            Root->MOTOR_BOTTOM_CHASSIS_3        == RUI_DF_OFFLINE ||
            Root->MOTOR_BOTTOM_CHASSIS_4		== RUI_DF_OFFLINE))
            {
                RUI_F_LED(RUI_DF_LED_CYAN , 50);
            }
        }
        break;
    }
}
